/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.ejbs;

import cassys.application.DTOs.AssessmentDTO;
import cassys.application.DTOs.AssessmentGroupDTO;
import cassys.application.services.AssessmentManagementBeanRemote;
import cassys.data.AssessmentDAO;
import cassys.data.AssessmentGroupDAO;
import cassys.data.DAOFactory;
import cassys.data.ModuleDAO;
import cassys.data.StudentDAO;
import cassys.domain.assessment.Assessment;
import cassys.domain.assessment.AssessmentGroup;
import cassys.domain.assessment.AssessmentItem;
import cassys.domain.assessment.AssessmentType;
import cassys.domain.assessment.MarkRange;
import cassys.domain.classManagement.ClassGroup;
import cassys.domain.classManagement.ClassRegistration;
import cassys.domain.classManagement.Module;
import cassys.domain.classManagement.Student;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

/**
 *
 * @author User
 */
@Stateless(mappedName = "AssessmentManagementBean")
public class AssessmentManagementBean implements AssessmentManagementBeanRemote {

    DAOFactory daoFactory = DAOFactory.instance(DAOFactory.HIBERNATE);
    ModuleDAO moduleDAO;
    AssessmentDAO assessmentDAO;
    AssessmentGroupDAO assessmentGroupRepository;
    StudentDAO studentRepository;

    @PostConstruct
    public void init() {
        System.out.println("Post Construct : Assessment Manager Bean");
        moduleDAO = daoFactory.getModuleDAO();
        assessmentDAO = daoFactory.getAssessmentDAO();
        assessmentGroupRepository = daoFactory.getAssessmentGroupDAO();
    }

    @Override
    public List<Assessment> getAllAssessments() {
        return assessmentDAO.findAll();
    }

    @Override
    public void createAssessment(Assessment assessment) {
        try {

            System.out.println("Persisting info :" + assessment.toString());
            Module module = moduleDAO.findById(assessment.getModule().getModuleId(), false);
            Assessment newAssessment = new Assessment(module, assessment.getTitle(), assessment.getWeightPercentage());
            for (AssessmentItem assessmentItem : assessment.getAccessmentItems()) {
                newAssessment.addAccessmentItem(assessmentItem);
            }
            assessmentDAO.makePersistent(assessment);
            System.out.println("Persistent done");

        } catch (Exception e) {
            System.out.println("err:" + e.getMessage());
            System.out.println("err:" + e.getStackTrace());
        }
    }

    private Assessment mapNewAssessment(AssessmentDTO assessmentDTO) {
        Module assessmentModule = moduleDAO.findById(assessmentDTO.getModuleId(), false);

        Assessment newAssessment = new Assessment(assessmentModule, assessmentDTO.getTitle(), assessmentDTO.getWeightPercentage());

        newAssessment.setMarkRange(new MarkRange(assessmentDTO.getMaxMarkRange(), assessmentDTO.getMinMarkRange()));

        newAssessment.setMaxMark(assessmentDTO.getMaxMark());

        return newAssessment;

    }

    private Assessment mapExistingAssessment(AssessmentDTO assessmentDTO) {
        Assessment existingAssessment = assessmentDAO.findById(assessmentDTO.getAssessmentId(), false);
        existingAssessment.setDescription(assessmentDTO.getDescription());
        existingAssessment.setMarkRange(null);
        existingAssessment.setMaxMark(0);
        existingAssessment.addAccessmentItem(null);
        return existingAssessment;

    }

    @Override
    public List<AssessmentType> getAssessmentTypes() {
        return Arrays.asList(AssessmentType.values());
    }

    @Override
    public void updateAssessment(Assessment assessment) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteAssessment(Assessment assessment) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Assessment findByAssessmentId(long assessmentId) {
        return this.assessmentDAO.findById(assessmentId, false);
    }

    @Override
    public void createAssessmentGroup(AssessmentGroupDTO groupDTO) {
        System.out.println("creating a new group");

        AssessmentGroup assessmentGroup = new AssessmentGroup();
        assessmentGroup.setTitle(groupDTO.getAssessmentGroupName());

        long[] studentId = groupDTO.getMembers();
        for (long sId : studentId) {
            assessmentGroup.addMember(studentRepository.findById(sId, false));
        }
        try {
            assessmentGroupRepository.makePersistent(assessmentGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void editAssessmentGroup(AssessmentGroupDTO groupDTO) {

        AssessmentGroup assessmentGroup = assessmentGroupRepository.findById(groupDTO.getAssessmentGroupId(), false);
        assessmentGroup.setTitle(groupDTO.getAssessmentGroupName());

        for (Student student : assessmentGroup.getMembers()) {
            assessmentGroup.removeStudent(studentRepository.findById(student.getStudentId(), false));
        }

        for (long newStudentId : groupDTO.getMembers()) {
            assessmentGroup.addMember(studentRepository.findById(newStudentId, false));
        }

        try {
            assessmentGroupRepository.makePersistent(assessmentGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public AssessmentGroup findAssessmentGroupById(long assessmentGroupId) {
        return assessmentGroupRepository.findById(assessmentGroupId, false);
    }

    @Override
    public void deleteAssessmentGroup(AssessmentGroupDTO groupDTO) {
        AssessmentGroup assessmentGroup = assessmentGroupRepository.findById(groupDTO.getAssessmentGroupId(), false);

        //TODO Delete of assessmentGroup;

    }

    public List<AssessmentGroup> getAllAssessmentGroup(long assessmentId) {
        return assessmentGroupRepository.getAssessmentGroup(assessmentId);
    }

    @Override
    public AssessmentGroup getGroupParticipent(long assessmentGroupId) {
        return assessmentGroupRepository.getAssessmentGroupById(assessmentGroupId);
    }

    @Override
    public List<Student> getAllUnGroupParticipent(long assessmentId) {
        List<Student> result = new ArrayList<Student>();
        List<AssessmentGroup> groups = assessmentGroupRepository.getAssessmentGroup(assessmentId);
        List<Long> groupStudentIds = new ArrayList<Long>();
        for (AssessmentGroup ag : groups) {
            for (Student student : ag.getMembers()) {
                groupStudentIds.add(student.getStudentId());
            }
        }

        ClassGroup classGroup = moduleDAO.getAllUnGroupParticipent(assessmentId);
        for (ClassRegistration classReg : classGroup.getClassMembers()) {
            Student classStudent = classReg.getStudent();
            if (!groupStudentIds.contains(classStudent.getStudentId())) {
                result.add(classStudent);
            }
        }
        return result;
    }
}
